package cn.donting.jme.expansion.builder.light;

import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.lmpl.FloatAttribute;
import cn.donting.jme.expansion.attribute.lmpl.Vector3fAttribute;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.utile.MathUtil;
import com.jme3.light.SpotLight;
import com.jme3.math.FastMath;
import javax.swing.*;
import java.util.List;

@Bean
public class SpotLightAttributeBuilder extends LightAttributeBuilder<SpotLight> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof SpotLight;
    }

    @Override
    public SpotLight newInstance() {
        SpotLight pointLight = new SpotLight();
        pointLight.setName("聚光灯");
        return pointLight;
    }

    @Override
    public Icon getIcon(int size) {
        return null;
    }

    @Override
    public Icon getActiveIcon(int size) {
        return null;
    }

    @Override
    public String getName() {
        return "聚光灯";
    }

    @Override
    public void attribute(List<Attribute> attributes, SpotLight spotLight) {
        super.attribute(attributes, spotLight);
        Vector3fAttribute position = new Vector3fAttribute("位置");
        position.setValue(spotLight.getPosition());
        position.setValueChangeListener(true, newValue -> {
            spotLight.setPosition(newValue);
        });
        attributes.add(position);

        Vector3fAttribute directionAttribute = new Vector3fAttribute();
        directionAttribute.setTitle("方向");
        directionAttribute.setValue(spotLight.getDirection());
        directionAttribute.setValueChangeListener(newValue -> {
            spotLight.setDirection(newValue);
        });
        attributes.add(directionAttribute);

        FloatAttribute spotRange = new FloatAttribute("范围");
        spotRange.setValue(spotLight.getSpotRange());
        spotRange.setValueChangeListener(newValue -> {
            spotLight.setSpotRange(newValue);
        });
        attributes.add(directionAttribute);

        FloatAttribute innerAngle = new FloatAttribute("内部角度");
        innerAngle.setValue(MathUtil.radianToAngle(spotLight.getSpotInnerAngle()));
        innerAngle.setMinValue(0.00000000001f);
        innerAngle.setMaxValue(FastMath.HALF_PI / FastMath.PI * 180 - 0.00000000001f);
        innerAngle.setValueChangeListener(newValue -> {
            spotLight.setSpotInnerAngle(MathUtil.angleToRadian(newValue));
        });
        attributes.add(innerAngle);


        FloatAttribute outerAngle = new FloatAttribute("外部角度");
        outerAngle.setValue(MathUtil.radianToAngle(spotLight.getSpotOuterAngle()));
        outerAngle.setMinValue(0.00000000001f);
        outerAngle.setMaxValue(FastMath.HALF_PI / FastMath.PI * 180 - 0.00000000001f);
        outerAngle.setValueChangeListener(newValue -> {
            spotLight.setSpotOuterAngle(MathUtil.angleToRadian(newValue));
        });
        attributes.add(outerAngle);
    }
}
